package com.vhall.component.service.interactive.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vhall.component.dao.interactive.DocumentStatusMapper;
import com.vhall.component.dao.interactive.DocumentsRoomMapper;
import com.vhall.component.entity.account.dto.IndexDto;
import com.vhall.component.entity.data.enums.SelectTimeIntervalOptionsEnum;
import com.vhall.component.entity.export.dto.AdminExportDocumentQueryCondition;
import com.vhall.component.entity.record.RecordEntityDTO;
import com.vhall.component.entity.room.RoomDocumentsEntity;
import com.vhall.component.framework.common.constant.CommonConstant;
import com.vhall.component.service.interactive.DocumentsInnerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * @author lsl
 * @since 2021/6/11
 */
@Service
public class DocumentsInnerServiceImpl implements DocumentsInnerService {


    @Autowired
    private DocumentStatusMapper documentStatusMapper;
    @Autowired
    private DocumentsRoomMapper documentsRoomMapper;

    @Override
    public Integer getDocumentStatusExist(String ilId, RecordEntityDTO recordEntityDTO) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(CommonConstant.DATETIME_FORMAT);
        String endTime = recordEntityDTO.getCreatedTime().format(formatter);
        Integer seconds = recordEntityDTO.getDuration() + 5;
        LocalDateTime start = recordEntityDTO.getCreatedTime().minusSeconds(seconds);
        String startTime = start.format(formatter);

        int count = documentStatusMapper.exist(ilId, startTime, endTime);
        if (count > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public IndexDto getCount(String beginTime) {
       return getCount(beginTime,null);
    }

    /**
     *
     * @param beginTime
     * @param orgCode
     * @return
     */
    @Override
    public IndexDto getCount(String beginTime, String orgCode) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(CommonConstant.DATETIME_FORMAT);
        LocalDateTime time = LocalDateTime.parse(beginTime, df);
        LocalDateTime today = LocalDate.now().atStartOfDay();
        QueryWrapper<RoomDocumentsEntity> queryWrapper = new QueryWrapper<>();
        Integer total = documentsRoomMapper.selectCount(queryWrapper);
        queryWrapper.clear();
        Integer day = documentsRoomMapper.selectCount(queryWrapper.ge("created_at", time));
        queryWrapper.clear();
        queryWrapper.le("created_at",today);
        Integer week = documentsRoomMapper.selectCount(queryWrapper.ge("created_at", time.plusDays(-1 * SelectTimeIntervalOptionsEnum.PAST_7_DAYS.getDay())));
        queryWrapper.clear();
        queryWrapper.le("created_at",today);
        Integer month = documentsRoomMapper.selectCount(queryWrapper.ge("created_at", time.plusDays(-1 * SelectTimeIntervalOptionsEnum.PAST_30_DAYS.getDay())));
        queryWrapper.clear();
        queryWrapper.le("created_at",today);
        Integer year = documentsRoomMapper.selectCount(queryWrapper.ge("created_at", time.minusYears(1L)));
        IndexDto indexDto = new IndexDto();
        indexDto.setTotal(total);
        indexDto.setDay(day);
        indexDto.setWeek(week);
        indexDto.setMonth(month);
        indexDto.setYear(year);
        return indexDto;
    }

    @Override
    public int countForAdminExport(AdminExportDocumentQueryCondition condition) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(condition)) {
            String createdAtColumn = "room_documents.created_at";
            // 开始日期、结束日期
            wrapper.ge(Objects.nonNull(condition.getStartTime()), createdAtColumn, condition.getStartTime());
            if (Objects.nonNull(condition.getEndTime())) {
                wrapper.lt(createdAtColumn, condition.getEndTime().plusDays(1));
            }
            String keyword = condition.getKeyword();
            // 模糊查询支持，文档名称
            wrapper.and(StringUtils.isNotEmpty(keyword), w -> w.like("room_documents.file_name", keyword));
            // 转码状态
            wrapper.eq(Objects.nonNull(condition.getTransStatus()), "room_documents.trans_status", condition.getTransStatus());
        }
        return documentsRoomMapper.countDocumentForAdmin(wrapper);
    }
}
